'use strict';
const db = uniCloud.database();
const userCollection = db.collection('xxf-users');
exports.main = async (event, context) => {
	//event为客户端上传的参数
	console.log('event : ', event);
	const {
		action
	} = event;
	switch (action) {
		case 'createUser':
			return createUser(event); // 创建用户
		case 'getUser':
			return getUser(event); // 查询用户
		case 'updateUser':
			return updateUser(event); // 更新用户
		case 'deleteUser':
			return deleteUser(event); // 删除用户
		case 'toBeVip':
			return toBeVip(event); // 开通会员
		default:
			return {
				msg: 'Invalid action！',
					code: 0,
					data: null
			};
	}
};

async function toBeVip(params) {
	const {
		uid,
	} = params;
	const updateRes = await userCollection.doc(uid).update({
		role: 2
	});
	console.log("updateRes: ", updateRes);
	if (updateRes.updated === 1) {
		const userRes = await userCollection.doc(uid).get();
		console.log("updateUser userRes2: ", userRes);
		const userObj = userRes.data[0];
		return {
			msg: 'success',
			code: 1,
			data: {
				...userObj
			}
		}
	} else {
		return {
			msg: 'fail！',
			code: 0,
			data: null
		}
	}
}
// 更新用户信息
async function updateUser(params) {
	const {
		uid
	} = params;
	if (uid) {
		let userRes = await userCollection.doc(uid).get();
		console.log("updateUser userRes0: ", userRes);
		if (userRes && userRes.data && userRes.data.length) {
			let userObj = userRes.data[0];
			console.log("updateUser userObj1: ", userObj);
			Object.keys(userObj).forEach((key) => {
				if (params[key]) {
					userObj[key] = params[key];
				}
			});
			console.log("updateUser userObj2: ", userObj);
			const updateRes = await userCollection.doc(uid).update({
				...userObj
			});
			console.log("updateRes res1: ", updateRes);
			if (updateRes.updated === 1) {
				userRes = await userCollection.doc(uid).get();
				console.log("updateUser res2: ", userRes);
				const userObj = userRes.data[0];
				return {
					msg: 'success',
					code: 1,
					data: {
						...userObj
					}
				}
			} else {
				return {
					msg: '更新失败！',
					code: 0,
					data: null
				}
			}
		} else {
			return {
				msg: '用户不存在！',
				code: 0,
				data: null
			};
		}
	} else {
		return {
			msg: '参数(uid)错误！',
			code: 0,
			data: null
		};
	}
}
// 获取用户信息
async function getUser(params) {
	const {
		uid
	} = params;
	if (uid) {
		const userRes = await userCollection.doc(uid).get();
		if (userRes && userRes.data && userRes.data.length) {
			return {
				msg: 'success',
				code: 1,
				data: {
					...userRes.data[0]
				}
			};
		} else {
			return {
				msg: '用户不存在！',
				code: 0,
				data: null
			};
		}
	} else {
		return {
			msg: '参数(uid)错误！',
			code: 0,
			data: null
		};
	}
}
// 添加用户
async function createUser(params) {
	const {
		token,
		open_id,
		session_key
	} = params;
	const createRes = await userCollection.add({
		open_id: open_id,
		session_key: session_key,
		token: token,
		create_time: Date.now(),
		latest_login_time: Date.now(),
		role: 1,
		nickname: "",
		avatar_url: "",
		gender: "",
		mobile: "",
		country: "",
		province: "",
		city: "",
		district: "",
		street: "",
		street_num: "",
	})
	console.log("createRes: ", createRes);
	if (createRes) {
		const userRes = await userCollection.doc(open_id).get();
		// const userRes = await userCollection.doc(open_id).field({
		// 	open_id: true,
		// 	token: true,
		// 	nickname: true,
		// }).get();
		console.log("userRes: ", userRes);
		const userObj = userRes.data[0];
		return {
			msg: 'success',
			code: 1,
			data: {
				...userObj
			}
		}
	} else {
		return {
			msg: 'fail',
			code: 0,
			data: null
		};
	}
}